﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using AutoMapper;
using PublishNews.Entities;
using PublishNews.Models;
using PublishNews.Repositories;

namespace PublishNews.Controllers
{
    public class BannerApiController : BaseApiController<Banner, BannerModel>
    {
        public BannerApiController(IBannerRepository repository)
            : base(repository)
        {
        }

        public override IHttpActionResult Get()
        {
            var entities = Repository.DbSet().OrderByDescending(x => x.UpdatedDate).ThenByDescending(x => x.CreatedDate);
            var model = Mapper.Map<IEnumerable<BannerModel>>(entities).ToList();
            Uri uri = HttpContext.Current.Request.Url;
            string host = "";
            if (uri.Port != 80)
            {
                host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;
            }
            else
            {
                host = uri.Scheme + Uri.SchemeDelimiter + uri.Host;
            }

            model.ForEach(x =>
                              {
                                  x.ImageUrl = host + x.ImageUrl;
                              });
            return Ok(model);
        }
    }
}
